//
//  HMAFHTTPClient.m
//  localLife
//
//  Created by Limonwei on 15/11/23.
//  Copyright © 2015年 heima. All rights reserved.
//

#import "HMAFHTTPClient.h"
#import "HMHomeModel.h"
#import "HMCircleModel.h"
#import "HMGoodsModel.h"

@implementation HMAFHTTPClient

+(id)shareInstance{
    
    
    static HMAFHTTPClient *client = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        //
        client = [[HMAFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:kBaseURL]];
        
        client.responseSerializer = [AFHTTPResponseSerializer serializer];
    });
    
    return client;
    
}









-(void)getHomeWithParams:(NSDictionary *)param
        withSuccessBlock:(successLoadData)success
         withFailedBlock:(failedLoadData)failed{
    
    
    [self getURL:@"?anu=api/1/index/get_index_info" withParams:param withSuccessBlock:^(id JSON) {
        //
        NSString *path = [[NSBundle mainBundle]pathForResource:@"home" ofType:@"plist"];
        
        NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
        
        HMHomeModel *model = [[HMHomeModel alloc]initWithDictionary:dict error:nil];
        
        success(model);
        
    } withFailedBlock:failed];
    
}


+(void)getHomeWithParams:(NSDictionary *)param
        withSuccessBlock:(successLoadData)success
         withFailedBlock:(failedLoadData)failed{
    
    [[self shareInstance]getHomeWithParams:param withSuccessBlock:success withFailedBlock:failed];
    
}

-(void)getClrcleWithParams:(NSDictionary *)param
          withSuccessBlock:(successLoadData)success
           withFailedBlock:(failedLoadData)failed{
    
    [self getURL:@"?anu=api/1/index/get_index_info" withParams:param withSuccessBlock:^(id JSON) {
        //
        NSString *path = [[NSBundle mainBundle]pathForResource:@"circle" ofType:@"plist"];
        
        NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
        
        HMCircleModelList *model = [[HMCircleModelList alloc]initWithDictionary:dict error:nil];
        
        success(model);
        
    } withFailedBlock:failed];
    
}

+(void)getClrcleWithParams:(NSDictionary *)param
          withSuccessBlock:(successLoadData)success
           withFailedBlock:(failedLoadData)failed{
    
    [[self shareInstance] getClrcleWithParams:param withSuccessBlock:success withFailedBlock:failed];
}

+(void)getGoodsWithParams:(NSDictionary *)param
         withSuccessBlock:(successLoadData)success
          withFailedBlock:(failedLoadData)failed{
    
    [[self shareInstance] getGoodsWithParams:param withSuccessBlock:success withFailedBlock:failed];
    
}


-(void)getGoodsWithParams:(NSDictionary *)param
         withSuccessBlock:(successLoadData)success
          withFailedBlock:(failedLoadData)failed{
    
    
    [self getURL:@"?anu=api/1/index/get_index_info" withParams:param withSuccessBlock:^(id JSON) {
        //
        NSString *path = [[NSBundle mainBundle]pathForResource:@"good_info" ofType:@"plist"];
        
        NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
        
        HMGoodsModel *model = [[HMGoodsModel alloc]initWithDictionary:dict error:nil];
        
        success(model);
        
    } withFailedBlock:failed];
    

    
}






-(void)getURL:(NSString *)url
   withParams:(NSDictionary *)param
withClassName:(NSString *)name
withSuccessBlock:(successLoadData)success
withFailedBlock:(failedLoadData)failed{
    
    
    [self GET:url parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        //
        
        Class cl = NSClassFromString(name);
        
        success([[cl alloc]initWithDictionary:responseObject]);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //
        
        failed(error);
    }];
    
}

+(void)getURL:(NSString *)url
   withParams:(NSDictionary *)param
withSuccessBlock:(successLoadData)success
withFailedBlock:(failedLoadData)failed{
    
    [[self shareInstance]getURL:url withParams:param withSuccessBlock:success withFailedBlock:failed];
    
}

-(void)getURL:(NSString *)url
   withParams:(NSDictionary *)param
withSuccessBlock:(successLoadData)success
withFailedBlock:(failedLoadData)failed{
    
    
    [self GET:url parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        //
        success(responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //
        failed(error);
    }];
    
}


-(void)postURL:(NSString *)url
    withParams:(NSDictionary *)param
withSuccessBlock:(successLoadData)success
withFailedBlock:(failedLoadData)failed{
    
    
    [self POST:url parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        //
        success(responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //
        failed(error);
    }];
    
}


+(void)postURL:(NSString *)url
    withParams:(NSDictionary *)param
withSuccessBlock:(successLoadData)success
withFailedBlock:(failedLoadData)failed{
    
    [[self shareInstance]postURL:url withParams:param withSuccessBlock:success withFailedBlock:failed];
    
}
@end























